1   /*
2    * Copyright (C) 2008 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.collect.testing.testers;
18  
19  import static com.google.common.collect.testing.features.CollectionSize.ZERO;
20  import static com.google.common.collect.testing.features.MapFeature.*;
21  
22  import com.google.common.annotations.GwtCompatible;
23  import com.google.common.collect.testing.AbstractMapTester;
24  import com.google.common.collect.testing.WrongType;
25  import com.google.common.collect.testing.features.CollectionSize;
26  import com.google.common.collect.testing.features.MapFeature;
27  
28  /**
29   * A generic JUnit test which tests {@code containsValue()} operations on a map.
30   * Can't be invoked directly; please see
31   * {@link com.google.common.collect.testing.MapTestSuiteBuilder}.
32   *
33   * @author George van den Driessche
34   * @author Chris Povirk
35   */
36  @GwtCompatible
37  public class MapContainsValueTester<K, V> extends AbstractMapTester<K, V> {
38    @CollectionSize.Require(absent = ZERO)
39    public void testContains_yes() {
40      assertTrue("containsValue(present) should return true",
41          getMap().containsValue(samples.e0.getValue()));
42    }
43  
44    public void testContains_no() {
45      assertFalse("containsValue(notPresent) should return false",
46          getMap().containsValue(samples.e3.getValue()));
47    }
48  
49    @MapFeature.Require(ALLOWS_NULL_VALUE_QUERIES)
50    public void testContains_nullNotContainedButAllowed() {
51      assertFalse("containsValue(null) should return false",
52          getMap().containsValue(null));
53    }
54  
55    @MapFeature.Require(absent = ALLOWS_NULL_VALUE_QUERIES)
56    public void testContains_nullNotContainedAndUnsupported() {
57      expectNullValueMissingWhenNullValuesUnsupported(
58          "containsValue(null) should return false or throw");
59    }
60  
61    @MapFeature.Require(ALLOWS_NULL_VALUES)
62    @CollectionSize.Require(absent = ZERO)
63    public void testContains_nonNullWhenNullContained() {
64      initMapWithNullValue();
65      assertFalse("containsValue(notPresent) should return false",
66          getMap().containsValue(samples.e3.getValue()));
67    }
68  
69    @MapFeature.Require(ALLOWS_NULL_VALUES)
70    @CollectionSize.Require(absent = ZERO)
71    public void testContains_nullContained() {
72      initMapWithNullValue();
73      assertTrue("containsValue(null) should return true",
74          getMap().containsValue(null));
75    }
76  
77    public void testContains_wrongType() {
78      try {
79        //noinspection SuspiciousMethodCalls
80        assertFalse("containsValue(wrongType) should return false or throw",
81            getMap().containsValue(WrongType.VALUE));
82      } catch (ClassCastException tolerated) {
83      }
84    }
85  }